package com.ruoyi.web.controller.kycinfo;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.kycinfo.service.IKycService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kycinfo.domain.KycLabel;
import com.ruoyi.kycinfo.service.IKycLabelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * kyc用户标签Controller
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@RestController
@RequestMapping("/kycinfo/label")
public class KycLabelController extends BaseController {
    @Autowired
    private IKycLabelService kycLabelService;

    @Autowired
    private IKycService kycService;

    /**
     * 查询kyc用户标签列表
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:label:list')")
    @GetMapping("/list")
    public TableDataInfo list(KycLabel kycLabel) {
        // todo 添加权限控制，只能查询到自己打的客户标签
        Long userId = SecurityUtils.getUserId();
        kycLabel.setUserId(userId);
        Long userInfoId = kycLabel.getUserInfoId();
        if (userInfoId != null) {
            List<Long> userInfoIdList = kycService.selectKycUserInfoIdList();
            if (!userInfoIdList.contains(userInfoId)) {
                return getDataTable(new ArrayList<>());
            }
        }
        startPage();
        List<KycLabel> list = kycLabelService.selectKycLabelList(kycLabel);
        return getDataTable(list);
    }

    /**
     * 导出kyc用户标签列表
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:label:export')")
    @Log(title = "kyc用户标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KycLabel kycLabel) {
        List<KycLabel> list = kycLabelService.selectKycLabelList(kycLabel);
        ExcelUtil<KycLabel> util = new ExcelUtil<KycLabel>(KycLabel.class);
        util.exportExcel(response, list, "kyc用户标签数据");
    }

    /**
     * 获取kyc用户标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:label:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(kycLabelService.selectKycLabelById(id));
    }

    /**
     * 新增kyc用户标签
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:label:add')")
    @Log(title = "kyc用户标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KycLabel kycLabel) {
        return toAjax(kycLabelService.insertKycLabel(kycLabel));
    }

    /**
     * 修改kyc用户标签
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:label:edit')")
    @Log(title = "kyc用户标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KycLabel kycLabel) {
        return toAjax(kycLabelService.updateKycLabel(kycLabel));
    }

    /**
     * 删除kyc用户标签
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:label:remove')")
    @Log(title = "kyc用户标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(kycLabelService.deleteKycLabelByIds(ids));
    }
}
